package com.goingup.domain;

import java.util.ArrayList;

import com.goingup.domain.Vector.Direction;


public class RequestController {
	private ArrayList<Requestable> requestables;

	public RequestController()
	{
		requestables=new ArrayList<Requestable>();
	}
	public void addRequestable(Requestable requestable)
	{
		requestables.add(requestable);
	}
	/**
	 * Adds the pickup request to the closest elevator.
	 * @param floor
	 * @param direction
	 */
	public void requestPickupOn(Floor floor, Direction direction)
	{
		//give the request to the closest elevator
		//moving in the same direction as the request
		System.out.print("Rquest pickup for floor: " +floor.getNumber() + " in direction: "+ direction + "\n");
		int currentDistance=99999;
		Requestable closestServiceProvider = requestables.get(0);
		for (Requestable requestable : requestables) {
			if (!requestable.acceptingRequests())
				continue;
			int nextDistance = requestable.distanceFrom(floor);
			if (currentDistance > nextDistance)
			{
				currentDistance =nextDistance;
				closestServiceProvider=requestable;
			}
		}
		closestServiceProvider.addRequest(new PickUpRequest(floor, direction));
	}
	
}
